//
//  YDOAHomeVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/5/26.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import HandyJSON
class YDOAHomeVC: UIViewController {

    @IBOutlet weak var userInfoLab: UILabel!
    @IBOutlet weak var weatherLab: UILabel!
    @IBOutlet weak var contentView: UIView!
    @IBOutlet weak var myTableView: UITableView!
    
    var dataSource : [String : AnyObject]?
    lazy var lunchData : NSMutableArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
       // loadLunchData()
        setupUIWith(dataSource: dataSource!)

        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        myTableView.register(UINib(nibName: "YDLunchCell", bundle: nil), forCellReuseIdentifier: "YDLunchCell")
        myTableView.register(UINib(nibName: "YDOAHomeCell1", bundle: nil), forCellReuseIdentifier: "YDOAHomeCell1")
        myTableView.register(UINib(nibName: "YDOAHomeCell2", bundle: nil), forCellReuseIdentifier: "YDOAHomeCell2")
        
        myTableView.separatorStyle = .none
        
        // Do any additional setup after loading the view.
    }
    
//    加载今日勾餐信息
   func loadLunchData(){
        let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
        let token = "token" //userinfo["token"] as! String
        let id = userinfo["id"] as? Int
    let urlStr = String(format:GET_TODAY_LUNCH_LIST,id!)
        YMLog.log(item: urlStr)
        YMNetworkManager().getRequest(urlString: urlStr, params: nil, success: { (responser) in
            if responser["status"]?.int8Value == 1{
                let dataArr = responser["data"] as! NSArray
                for dict  in dataArr{
                    guard let model =  JSONDeserializer<YDOALunchItemModel>.deserializeFrom(dict: dict as? NSDictionary) else {return}
                    self.lunchData.add(model)
                }
                self.myTableView.reloadData()
            }
        }) { (error) in
        
        }
    }
    

//    创建UI 界面
    func setupUIWith(dataSource:[String : AnyObject]) {
        guard let position = dataSource["ment"],
            let user_name = dataSource["uname"]
            else { return }
        userInfoLab.text = "\(position) : \(user_name)"
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //进入首页
    @IBAction func gotoNav(_ sender: Any) {
        
        let nav = YDNav(rootViewController: YDOAFuncVC())
        
        self.present(nav, animated: true) { 
            
        }
    }

    
    
    //退出登录
    @IBAction func toBack(_ sender: Any) {
//        self.present(YDOALoginVC(), animated: true) { 
//            
//        }
        self.dismiss(animated: true) { 
            
        }
    }
    
    
    
}



extension YDOAHomeVC : UITableViewDelegate,UITableViewDataSource {

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 4
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return lunchData.count
        }
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.01
        }
        return 10
        
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     
        
        if indexPath.section == 0{
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDLunchCell") as? YDLunchCell  else {
                return YDLunchCell()
            }
            
            cell.contentView.backgroundColor = UIColor.clear
            cell.selectionStyle = .none
            let model = self.lunchData[indexPath.row] as! YDOALunchItemModel
            cell.model = model
            //点击A或B套餐时触发事件
            weak var weakSelf = self
            cell.callbackWithType = { (type : Type , title : String? , id : Int? , time : String?) in
                let vc =  LoginPresentVC()
                if type == Type.A {
                    vc.msg = "你确定选择A套餐吗？\n \(title!)"
                    
                }else{
                    vc.msg = "你确定选择B套餐吗？\n \(title!)"
                }
                vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
                vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
                vc.sureCallBack = {() in
//                    YMLog.log(item: "勾餐名称: \(title)   勾餐的 ID \(id)")
                    let userinfo = UserDefaults.standard.object(forKey: USERINFO) as! [String:AnyObject]
                    let token = "token" //userinfo["token"] as! String
                    let user_id = userinfo["id"] as! Int
                    let params = [
                        "token":token ,
                        "user_id":user_id ,
                        "time":time ?? "",
                        "can":title ?? "",
                        ] as [String : Any]
                    YMLog.log(item: params)
                    YMNetworkManager().postRequset(urlString: ORDER_LUNCH, params: params, success: { (responser) in
                        if responser["status"]?.int8Value == 1{
                            YMLog.log(item: responser)
                        }
                    }) { (error) in
                        
                    }
                }
                
                weakSelf?.present(vc, animated: true, completion: {
                    
                })
            }
            return cell

        }else if indexPath.section == 1{
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDOAHomeCell1") as? YDOAHomeCell1  else {
                return YDOAHomeCell1()
            }
            
            cell.contentView.backgroundColor = UIColor.clear
            cell.selectionStyle = .none
            return cell
            //YDOAHomeCell1
        }else if indexPath.section == 2{
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDOAHomeCell2") as? YDOAHomeCell2  else {
                return YDOAHomeCell2()
            }
            
            cell.contentView.backgroundColor = UIColor.clear
            cell.selectionStyle = .none
            cell.setupUI(title: "您有新的待审批事项")
            return cell
        }else{
            guard let cell = tableView.dequeueReusableCell(withIdentifier: "YDOAHomeCell2") as? YDOAHomeCell2  else {
                return YDOAHomeCell2()
            }
            
            cell.contentView.backgroundColor = UIColor.clear
            cell.selectionStyle = .none
            return cell
        }
        
        
        
//        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
//            return UITableViewCell()
//        }
//        
//        cell.selectionStyle = .none
//        cell.textLabel?.text = "我只是一个列表而已"
        return UITableViewCell()
        
    }

}




